<?php
class MUser extends CI_Model{

	public function __construct()
    {
        parent::__construct();
    }


	function verifyUser($u,$pw){
		$this->db->select('UserId,Username,UserTypeID,StaffId');
		$this->db->where('Username',db_clean($u,50));
		$this->db->where('Password', db_clean(do_hash($pw),16));
		$this->db->where('Activate', '1');
		$this->db->limit(1);
		$Q = $this->db->get('user');

		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			$_SESSION['uid'] = $row['UserId'];
			$_SESSION['role_id'] = $row['UserTypeID'];
			$_SESSION['username'] = $row['Username'];
		}else{
			 $_SESSION['uid'] = 0; 
			 $_SESSION['role_id'] = 0;
			$this->session->set_flashdata('error', 'Sorry, your username or password is incorrect!');
		}		
	}
	function getRoles() {
		 $data = array();
		  $this->db->order_by("UserType", "DESC");
	     $Q = $this->db->get('usertype');
	     if ($Q->num_rows() > 0){
	       foreach ($Q->result_array() as $row){
	         $data[$row['UserTypeID']] = $row['UserType'];
	       }
	     }
	     $Q->free_result();    
	     return $data; 		
	}
	function getUser($id) {
		$data = array();
	    $this->db->where('UserId',id_clean($id));
	    $this->db->limit(1);
	    $Q = $this->db->get('user');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;      
	}
	
	function addUser(UserModel $userModel){
        //$this->load->library('usermodel');
        $data = array('Username' => db_clean($userModel->getUserName()),
                    'Password' => db_clean(do_hash($userModel->getPassword()),16),
                    'Email' => db_clean($userModel->getEmail()),
      				'Phone' => db_clean($userModel->getPhone()),
                    'UserTypeID'=>id_clean($userModel->getUserTypeID()),
                    'StaffId'=>id_clean($userModel->getStaffID()),
                    'Activate' => db_clean($userModel->getActivate())
                    );
	   $this->db->set('CreatedDate', 'NOW()', FALSE);
	   $this->db->insert('user',$data);
	}
	
	function updateUser(UserModel $userModel){
	   if($userModel->getPassword() != null || $userModel->getPassword() != '')
       {
	       $data = array('Username' => db_clean($userModel->getUserName()),
                    'Password' => db_clean(do_hash($userModel->getPassword()),16),
                    'Email' => db_clean($userModel->getEmail()),
      				'Phone' => db_clean($userModel->getPhone()),
                    'UserTypeID'=>id_clean($userModel->getUserTypeID()),
                    'Activate' => db_clean($userModel->getActivate())
                    );
	   }
       else{
         $data = array('Username' => db_clean($userModel->getUserName()),
                'Email' => db_clean($userModel->getEmail()),
  				'Phone' => db_clean($userModel->getPhone()),
                'UserTypeID'=>id_clean($userModel->getUserTypeID()),
                'Activate' => db_clean($userModel->getActivate())
                );
       }
       $this->db->set('ModifiedDate', 'NOW()', FALSE);
	   $this->db->where('StaffId',id_clean($userModel->getStaffID()));
	   $this->db->update('user',$data);	
	}
	
	function activateUser($id){
 	 $data = array('activate' => db_clean($_POST['activate']));
 	 $this->db->where('uid', id_clean($id));
	 $this->db->update('user', $data);
	
	}
	
	function inActiveUser($id){
 	 $data = array('activate' => db_clean('0'));
 	 $this->db->where('uid', id_clean($id));
	 $this->db->update('user', $data);
	
	}
}


?>